package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.entity.MPage;
import com.xxx.entity.Reservation;
import com.xxx.entity.query.ReservationQuery;
import com.xxx.service.IReservationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-24
 */
@RestController
@RequestMapping("/reservation")
@Tag(name = "预约管理")
public class ReservationController {
    @Autowired
    private IReservationService reservationService;

    @PostMapping("/add")
    @Operation(summary = "添加预约")
    public Result add(@RequestBody Reservation reservation){
        Boolean flag = reservationService.save(reservation);
        return flag ? Result.success("添加预约成功") : Result.error("添加预约失败");
    }

    @GetMapping("/delete/{reservationId}")
    @Operation(summary = "删除预约")
    public Result delete(@PathVariable Integer reservationId){
        Boolean flag = reservationService.removeById(reservationId);
        return flag ? Result.success("删除预约成功") : Result.error("不存在该预约Id");
    }

    @PutMapping("/update")
    @Operation(summary = "修改预约")
    public Result update(@RequestBody Reservation reservation){
        Boolean flag = reservationService.updateById(reservation);
        return flag ? Result.success("修改预约成功") : Result.error("修改预约失败");
    }

    @GetMapping("/selectByPage/{current}/{size}")
    @Operation(summary = "分页查询预约")
    public Result selectByPage(@PathVariable Integer current, @PathVariable Integer size){
//        IPage<Reservation> page = reservationService.page(new Page<>(current,size));
        MPage page = reservationService.selectByPage(current, size);
        return Result.success(page);
    }

    @PostMapping("/selectByCondition")
    @Operation(summary = "根据状态、用户Id、预约Id条件查询预约")
    public Result selectByCondition(@RequestBody ReservationQuery reservationQuery){
//        LambdaQueryWrapper<Reservation> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(reservationQuery.getReservationStatus()!=null,
//                        Reservation::getReservationStatus,reservationQuery.getReservationStatus())
//                .eq(reservationQuery.getUserId()!=null,
//                        Reservation::getUserId,reservationQuery.getUserId())
//                .eq(reservationQuery.getReservationId()!=null,Reservation::getReservationId,reservationQuery.getReservationId());
//        IPage<Reservation> page = reservationService.page(new Page<>(reservationQuery.getCurrent(), reservationQuery.getSize()),
//                lqw);
        MPage page = reservationService.selectByCondition(reservationQuery);
        return Result.success(page);
    }
}

